/*
 * Developer : Jack Matthews
 * Email: developer@droidprofessor.com
 * 
 * Copyright (C) 2010 http://droidprofessor.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.droidprofessor.android.library.phonelicenses;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

abstract class XmlParsableSaxDefaultHandler<T> extends DefaultHandler implements XmlParsable<T> {
	private static final String TAG = "XmlParsableSaxDefaultHandler";
	
	/* Get a SAXParser from the SAXPArserFactory. */
	private static SAXParserFactory spf;
	private static SAXParser sp;
	private static XMLReader xr;
	
	static {
		try {
			spf = SAXParserFactory.newInstance();
			sp = spf.newSAXParser();
			xr = sp.getXMLReader();
		} catch (FactoryConfigurationError e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}
	
	
	@Override
	public void parse(InputStream in) {
		xr.setContentHandler(this);
		
		try {
			xr.parse(new InputSource( in ));
		} catch (IOException e) {
			Log.e(TAG, "IOException", e);
		} catch (SAXException e) {
			Log.e(TAG, "SAXException", e);
		}
	}

}
